package com.shouer.doc.ui.forgetpassword.presenter;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.forgetpassword.response.CheckSMSResponse;
import com.shouer.doc.ui.forgetpassword.response.ForgetPassResponse;
import com.shouer.doc.ui.forgetpassword.response.GetSmsResponse;
import com.shouer.doc.ui.forgetpassword.view.IForgetPassView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZXL on 2016/12/8.
 */

public class ForgetPassPresenterCompl implements IForgetPassPresenter {

    private IForgetPassView forgetPassView;

    public ForgetPassPresenterCompl(IForgetPassView forgetPassView) {
        this.forgetPassView = forgetPassView;
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        this.forgetPassView = null;
    }

    @Override
    public void getSMS(String userPhone) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", userPhone);

        final GsonRequest<GetSmsResponse> request = new GsonRequest("getSMS", GetSmsResponse.class, new Response.Listener<GetSmsResponse>() {
            @Override
            public void onResponse(GetSmsResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                forgetPassView.getSmsFailed("网络异常 获取校验码失败");
            }
        });
        RequestUtil.sendRequest(forgetPassView.getContext(), param, request);
    }

    @Override
    public void checkSMS(String userPhone, String sms) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", userPhone);
        param.put("sms", sms);

        final GsonRequest<CheckSMSResponse> request = new GsonRequest("checkSMS", CheckSMSResponse.class, new Response.Listener<CheckSMSResponse>() {
            @Override
            public void onResponse(CheckSMSResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                forgetPassView.checkNumFailed("网络异常 校验失败");
            }
        });
        RequestUtil.sendRequest(forgetPassView.getContext(), param, request);
    }

    @Override
    public void forget(String userPhone, String password) {
        Map<String, String> param = new HashMap<>();
        param.put("userPhone", userPhone);
        param.put("password", password);
        param.put("operType", "1");
        param.put("loginFlag", "2");


        final GsonRequest<ForgetPassResponse> request = new GsonRequest("modifyPassword", ForgetPassResponse.class, new Response.Listener<ForgetPassResponse>() {
            @Override
            public void onResponse(ForgetPassResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                forgetPassView.forgetFailed("网络异常 修改密码失败");
            }
        });
        RequestUtil.sendRequest(forgetPassView.getContext(), param, request);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ForgetPassResponse response) {

        if (response.getErrorCode() >= 0) {
            forgetPassView.forgetSuccess();
        } else {
            forgetPassView.forgetFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GetSmsResponse response) {
        if (response.getErrorCode() >= 0) {
            forgetPassView.getSmsSuccess();
        } else {
            forgetPassView.getSmsFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CheckSMSResponse response) {

        if (response.getErrorCode() >= 0) {
            forgetPassView.checkNumSuccess();
        } else {
            forgetPassView.checkNumFailed(response.getErrorMsg());
        }
    }
}
